package com.hz.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.hz.HtmlToWord;
import org.apache.commons.lang.StringEscapeUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Map;
import java.util.Set;

/**
 * Created by kaddddd on 2018/4/28 0028.
 */
@Controller
@RequestMapping(value = "/createWord")
public class WordController {
    @RequestMapping("")
    @ResponseBody
    public String createWord(HttpServletRequest request){
        //多个参数的话只要多个@RequestParam即可，注意参数名要和表单里面的属性名一致
        JSONObject json =new JSONObject();
        System.out.println("getRequestURL: "+request.getRequestURL());
        System.out.println("getRequestURI: "+request.getRequestURI());
        System.out.println("getQueryString: "+request.getQueryString());
        System.out.println("getRemoteAddr: "+request.getRemoteAddr());
        System.out.println("getRemoteHost: "+request.getRemoteHost());
        System.out.println("getRemotePort: "+request.getRemotePort());
        System.out.println("getRemoteUser: "+request.getRemoteUser());
        System.out.println("getLocalAddr: "+request.getLocalAddr());
        System.out.println("getLocalName: "+request.getLocalName());
        System.out.println("getLocalPort: "+request.getLocalPort());
        System.out.println("getMethod: "+request.getMethod());
        System.out.println("-------request.getParamterMap()-------");
        //得到请求的参数Map，注意map的value是String数组类型
        Map map = request.getParameterMap();
        Set<String> keySet = map.keySet();
        for (String key : keySet) {
            String[] values = (String[]) map.get(key);
            for (String value : values) {
                System.out.println(key+"="+value);
            }
        }
        System.out.println("--------request.getHeader()--------");
        //得到请求头的name集合
        Enumeration<String> em = request.getHeaderNames();
        while (em.hasMoreElements()) {
            String name = (String) em.nextElement();
            String value = request.getHeader(name);
            System.out.println(name+"="+value);
        }
        String htmlContent = (String)request.getParameter("data");
        StringBuilder sb = new StringBuilder();
        try(BufferedReader reader = request.getReader();) {
            char[]buff = new char[1024];
            int len;
            while((len = reader.read(buff)) != -1) {
                sb.append(buff,0, len);
            }
        }catch (IOException e) {
            e.printStackTrace();
        }
        htmlContent = sb.toString();
        htmlContent.replace("&lt;", "<");
        htmlContent.replace("&gt;", ">");

        htmlContent = StringEscapeUtils.unescapeHtml(htmlContent);
        json.put("flag", true);
        json.put("message", "success");
        System.out.println(json.toJSONString());
        HtmlToWord.writeWordFile(htmlContent);
        return json.toJSONString();
    }
}
